home *** CD-ROM | disk | FTP | other *** search
/ Sport Stretch Interactive Stretching / Sport Stretch Interactive Stretching.iso / pc / PGFINISH.DIR / 00045_Constrain to Line.ls < prev    next >
Encoding:
Text File  |  2000-08-24  |  8.2 KB  |  157 lines

  1. property pSprite, pSpriteNum, pActive, pVector, pVectorLength, pDestination, pOrigin, pQTSpeed, pInited, pPosition, pDirection, pDistance, pPointH, pPointV, pBroadcast
  2.  
  3. on getBehaviorDescription me
  4.   return "CONSTRAIN TO LINE" & RETURN & RETURN & "[Advanced]" & RETURN & RETURN & "Creates a slider constrained to a straight line." & RETURN & RETURN & "Place the sprite at a position indicating the lowest value of the slider (typically the leftmost position) and use the behavior parameters to determine the direction the slider may be dragged." & RETURN & RETURN & "There are two methods to determine the direction of the slider:" & RETURN & RETURN & "The simplest is to select a direction (such as 'right') and a distance (in pixels) the slider can move. " & "Presets are available for each 45 degree angle." & RETURN & RETURN & "The other method is to choose 'point' and a coordinate for the sprite to move toward, allowing movements on other angles. " & "The initial position for the sprite to appear along the path is set by a number from 0 to 1 (default is 0.5, halfway between the maximum and minimum positions on the path)." & RETURN & RETURN & "The behavior broadcasts its current setting (always a number between 0 and 1) if the broadcast flag is set to anything but 0. " & "To intercept the broadcast, place an mConstrainedValue handler in a behavior on the target sprite." & RETURN & RETURN & "Example:" & RETURN & "  on mConstrainedValue(me, vSlider, vValue)" & RETURN & RETURN & "vSlider is the sprite number of the slider sending the message (enabling tests if there is more than one slider operating); vValue is the slider's current position." & RETURN & RETURN & "Use the mSetConstrainPos method to set the position of the slider from another handler. " & "For example," & RETURN & RETURN & "  sendSprite(sliderSpriteNum, #mSetConstrainPos, 0.66, FALSE)" & RETURN & RETURN & "would set the slider to a position two-thirds of the way between minimum and maximum positions without sending a new message to the broadcast sprite." & RETURN & RETURN & "The mIncrementConstrainPos handler can move the slider a set distance. " & "For example," & RETURN & RETURN & "  sendSprite(sliderSprite, #mIncrementConstrainPos, 0.05)" & RETURN & RETURN & "would move the sprite 1/20th of the distance from the minimum position to the maximum position. " & "Negative values move toward the minimum position." & RETURN & RETURN & "PERMITTED MEMBER TYPES:" & RETURN & "bitmap, Flash, QuickTime" & RETURN & RETURN & "PARAMETERS:" & RETURN & "* Position slider first appears on path" & RETURN & "* Direction sprite will slide" & RETURN & "* Distance sprite moves (used for relative direction)" & RETURN & "* Vertical point coordinate (used for specific point)" & RETURN & "* Horizontal point coordinate (used for specific point)" & RETURN & "* Broadcast flag for slider position"
  5. end
  6.  
  7. on getBehaviorTooltip me
  8.   return "[ADVANCED]" & RETURN & RETURN & "Makes a sprite draggable along a straight line." & RETURN & RETURN & "Place a sprite at one end of the line it should be dragged along, set parameters to determine the other end of the line, and where the draggable sprite should first appear. " & "The behavior can report its position along the line as a message to another sprite."
  9. end
  10.  
  11. on beginSprite me
  12.   mInitialize(me)
  13. end
  14.  
  15. on prepareFrame me
  16.   mUpdate(me)
  17. end
  18.  
  19. on mouseDown me
  20.   pActive = 1
  21.   if sprite(pBroadcast).member.type = #quickTimeMedia then
  22.     pQTSpeed = sprite(pBroadcast).movieRate
  23.     sprite(pBroadcast).movieRate = 0
  24.   end if
  25. end
  26.  
  27. on mInitialize me
  28.   pSpriteNum = me.spriteNum
  29.   pSprite = sprite(pSpriteNum)
  30.   pActive = 0
  31.   pOrigin = pSprite.loc
  32.   vStage = (the stage).rect
  33.   vDiagonal = integer(pDistance * sqrt(2.0) / 2)
  34.   case pDirection of
  35.     "Point":
  36.       pDestination = point(pPointH, pPointV)
  37.     "Up":
  38.       pDestination = point(pSprite.locH, max(0, pSprite.locV - pDistance))
  39.     "Down":
  40.       pDestination = point(pSprite.locH, min(vStage.height, pSprite.locV + pDistance))
  41.     "Left":
  42.       pDestination = point(max(0, pSprite.locH - pDistance), pSprite.locV)
  43.     "Right":
  44.       pDestination = point(min(vStage.width, pSprite.locH + pDistance), pSprite.locV)
  45.     "Up Left":
  46.       pDestination = point(max(0, pSprite.locH - vDiagonal), max(0, pSprite.locV - vDiagonal))
  47.     "Up Right":
  48.       pDestination = point(min(vStage.width, pSprite.locH + pDistance), max(0, pSprite.locV - vDiagonal))
  49.     "Down Left":
  50.       pDestination = point(max(0, pSprite.locH - vDiagonal), min(vStage.width, pSprite.locV + vDiagonal))
  51.     "Down Right":
  52.       pDestination = point(min(vStage.width, pSprite.locH + pDistance), min(vStage.width, pSprite.locV + vDiagonal))
  53.   end case
  54.   pVector = pDestination - pOrigin
  55.   pVectorLength = mVectorLength(pVector)
  56.   if sprite(pBroadcast).member.type = #quickTimeMedia then
  57.     pQTSpeed = sprite(pBroadcast).movieRate
  58.   else
  59.     pQTSpeed = 0
  60.   end if
  61.   vMember = pSprite.member
  62.   case vMember.type of
  63.     #animGif, #flash, #quickTimeMedia, #digitalVideo, #vectorShape:
  64.       if vMember.directToStage then
  65.         alert("Sprite" && pSpriteNum & ": Direct To Stage media may cause" && "playback problems with the 'Constrain to Line' behavior.")
  66.       end if
  67.   end case
  68.   mSetConstrainPos(me, pPosition, 1)
  69.   pInited = 0
  70. end
  71.  
  72. on mUpdate me
  73.   if not pInited then
  74.     mSetConstrainPos(me, pPosition, 1)
  75.     pInited = 1
  76.   end if
  77.   if pActive then
  78.     if the mouseUp then
  79.       pActive = 0
  80.       if sprite(pBroadcast).member.type = #quickTimeMedia then
  81.         sprite(pBroadcast).movieRate = pQTSpeed
  82.       end if
  83.     else
  84.       vDiff = pSprite.loc - the mouseLoc
  85.       vDist = mVectorLength(vDiff)
  86.       if vDist > 0 then
  87.         vPercent = min(1.0, vDist / pVectorLength) / 2
  88.         vAhead = min(pPosition + vPercent, 1.0)
  89.         vBehind = max(pPosition - vPercent, 0.0)
  90.         vAheadPoint = pOrigin + mScaleVector(pVector, vAhead)
  91.         vBehindPoint = pOrigin + mScaleVector(pVector, vBehind)
  92.         vAheadDist = mVectorLength(vAheadPoint - the mouseLoc)
  93.         vBehindDist = mVectorLength(vBehindPoint - the mouseLoc)
  94.         vCompare = vAheadDist - vBehindDist
  95.         if vCompare <> 0.0 then
  96.           if vCompare < 0 then
  97.             if vDist > vAheadDist then
  98.               mSetConstrainPos(me, vAhead, 1)
  99.             end if
  100.           else
  101.             if vDist > vBehindDist then
  102.               mSetConstrainPos(me, vBehind, 1)
  103.             end if
  104.           end if
  105.         end if
  106.       end if
  107.     end if
  108.   end if
  109. end
  110.  
  111. on mScaleVector vVector, vScale
  112.   return vVector * integer(1000 * vScale) / 1000
  113. end
  114.  
  115. on mVectorLength vVector
  116.   vSquare = (vVector.locH * vVector.locH) + (vVector.locV * vVector.locV)
  117.   return sqrt(float(vSquare))
  118. end
  119.  
  120. on mIncrementConstrainPos me, vIncrement
  121.   mSetConstrainPos(me, pPosition + vIncrement, 1)
  122. end
  123.  
  124. on mSetConstrainPos me, vPosition, vUpdate
  125.   pPosition = max(min(1.0, vPosition), 0.0)
  126.   pSprite.loc = pOrigin + mScaleVector(pVector, pPosition)
  127.   if pBroadcast then
  128.     if vUpdate then
  129.       sendSprite(pBroadcast, #mConstrainedValue, pSpriteNum, pPosition)
  130.     end if
  131.   end if
  132. end
  133.  
  134. on isOKToAttach me, aSpriteType, aSpriteNum
  135.   return aSpriteType = #graphic
  136. end
  137.  
  138. on getPropertyDescriptionList me
  139.   if not (the currentSpriteNum) then
  140.     exit
  141.   end if
  142.   vStage = (the stage).rect
  143.   vStageMeasure = max(vStage.height, vStage.width)
  144.   vPDList = [:]
  145.   setaProp(vPDList, #pDirection, [#comment: "Constraint direction (relative to current position)", #format: #string, #default: "Right", #range: mDirList()])
  146.   setaProp(vPDList, #pDistance, [#comment: "Distance (in pixels)", #format: #integer, #default: min(100, vStageMeasure), #range: [#min: 10, #max: vStageMeasure]])
  147.   setaProp(vPDList, #pPosition, [#comment: "Initial position on line (from 0 to 1)", #format: #float, #default: 0.5, #range: [#min: 0.0, #max: 1.0]])
  148.   setaProp(vPDList, #pPointH, [#comment: "Point Horizontal Value", #format: #integer, #default: vStage.width / 2, #range: [#min: 0, #max: vStage.width]])
  149.   setaProp(vPDList, #pPointV, [#comment: "Point Vertical Value", #format: #integer, #default: vStage.height / 2, #range: [#min: 0, #max: vStage.height]])
  150.   setaProp(vPDList, #pBroadcast, [#comment: "Broadcast position to sprite (0 = no broadcast)", #format: #integer, #default: 0, #range: [#min: 0, #max: the lastChannel]])
  151.   return vPDList
  152. end
  153.  
  154. on mDirList
  155.   return ["Point", "Up", "Down", "Left", "Right", "Up Left", "Up Right", "Down Left", "Down Right"]
  156. end
  157.